using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#region BaseDefine
public abstract class VActStateBase
{
    [TableList(AlwaysExpanded = true)]
    public List<VActStateUnit> states = new List<VActStateUnit>();
    protected VActProcessor processor;
    protected VActInput input;
    public void InitLoader(VActProcessor vActProcessor, VActInput vActInput)
    {
        processor = vActProcessor;
        input = vActInput;


        TriggerObserver TransDefault = new()
        {
            //end = processor.VS_AnimationGraph.TransDefault
        };
        input.AddAct("TransDefault", TransDefault, 0); //默认添加重返状态功能
        Load();
        //设置默认值, 只播放动画
        foreach (var state in states)
        {
            if (input.ExistAct(state.name)) continue;
            TriggerObserver trigger = new()
            {
                start = () => processor.ContinueState(state.assetClip)
            };
            input.AddAct(state.name, trigger, 0);
        }
    }
    public abstract void Load();
    /// <summary>
    /// 这里肯定得走局部配置的, 更加安全和持久化,并且支持热更
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public AnimationClip NameTo(string name)
    {
        var clip = states.Find(x => x.name == name)?.assetClip;
        if (clip != null) return clip;
        throw new Exception($"没有找到{name}定义的state动画！");
    }
}
[Serializable]
public class VActStateUnit
{
    public string name;
    public AnimationClip assetClip;
}
#endregion
public class VActStateBaseOverride : VActStateBase
{
    public override void Load()
    {
        FlowObserver Walk = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("Walk"));
            },
            end = () =>
            {
                processor.ContinueState();
            },
            tickType = 1,
        };
        input.AddAct("Walk", Walk, 11);

        FlowObserver Run = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("Run"));
            },
            end = () =>
            {
                processor.ContinueState();
            },
            tickType = 1,
        };
        input.AddAct("Run", Run, 20);
    }
}

public class VActState_AxeOverride : VActStateBase
{
    public override void Load()
    {
        FlowObserver walkForward = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("walkForward"));//就是说这里可以不同
            },
            end = () =>
            {
                processor.ContinueState();
            },
            tickType = 1,
        };
        input.AddAct("walkForward", walkForward, 11);
        FlowObserver walkBack = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("walkBack"));//就是说这里可以不同
            },
            end = () =>
            {
                processor.ContinueState();
            },
            tickType = 1,
        };
        input.AddAct("walkBack", walkBack, 11);

        TriggerObserver walkJump = new()
        {
            end = () =>
            {
                processor.VS_AnimationGraph.TriggerAnimationClip(NameTo("walkJump"));//这里是Trigger
            },
        };
        input.AddAct("walkJump", walkJump, 11);

        TriggerObserver hit = new()
        {
            end = () =>
            {
                processor.VS_AnimationGraph.TriggerAnimationClip(NameTo("hit"));
            },
        };
        input.AddAct("hit", hit, 900);
    }
}

public class VActState_MotionOverride : VActStateBase
{
    public override void Load()
    {

        FlowObserver walkBack = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("walkBack"));//就是说这里可以不同
                processor.mechine.SetSpeed(-2, 0, 0); // 这里可以按照恒定的帧速率加
            },
            end = () =>
            {
                processor.ContinueState();
                processor.mechine.Stop();
            },
            tickType = 1,
        };
        input.AddAct("walkBack", walkBack, 11);

        FlowObserver walkForward = new()
        {
            flowFrame = 0,
            tick = () =>
            {
                processor.ContinueState(NameTo("walkForward"));//就是说这里可以不同
                processor.mechine.SetSpeed(2, 0, 0); // 这里可以按照恒定的帧速率加
            },
            end = () =>
            {
                processor.ContinueState();
                processor.mechine.Stop();
            },
            tickType = 1,
        };
        input.AddAct("walkForward", walkForward, 11);


    }
}


